package com.stx.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.stx.dao.AdminDao;
import com.stx.dao.impl.TeacherDaoImpl.info;
import com.stx.domain.Admin;
import com.stx.domain.Teacher;
@Repository

public class AdminDaoImpl implements AdminDao {
	/*@Resource
	private Admin admin;*/
	@Resource
	private JdbcTemplate jt;
	
	public Admin adminLogin(String name,String pwd){		
		String sql="select * from A where NAME=? and PWD=?";
		Object[] obj={name,pwd};
		//返回值类型是ArrayList，不能用Admin等对象类型接收，否则报错
		List<Admin> list=jt.query(sql, obj, new info());
		//此处须将Object类型转换为相应的实体类型如Admin
		if(list.size()==1)
			return list.get(0);
		return null;
	}
		
	public void modifyAdminPwd(String name,String newpassword){
		jt.update("update A set PWD='"+newpassword+"' where NAME='"+name+"'");	
	}
	
	@SuppressWarnings("rawtypes")
	class info implements RowMapper{
		@Override
		public Object mapRow(ResultSet rs, int arg1) throws SQLException {
			Admin admin=new Admin();
			admin.setPwd(rs.getString("pwd"));
			admin.setName(rs.getString("name"));
			return admin;
		}
	}
}
